package com.hycat.mail.ware.mq.consume;

import com.hycat.mail.ware.mq.entity.UnLockStockTo;
import com.hycat.mail.ware.service.WareSkuService;
import com.hycat.mail.ware.service.impl.WareSkuServiceImpl;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * @author : lzhycat
 * @da te : 2022-08-25 14:15
 */
@Slf4j
@RabbitListener(queues = "stock.release.stock.queue")
public class UnLockStockConsume {
    private final static Logger logger = LoggerFactory.getLogger(UnLockStockConsume.class);

    @Autowired
    private WareSkuService wareSkuService;

    @RabbitHandler
    public void unLockHandler(UnLockStockTo unLockStockTo, Message message, Channel channel) throws IOException {
        logger.info("本次解锁库存的快照详情Id {}, 订单号 {}", unLockStockTo.getTaskDetailsId(), unLockStockTo.getOrderSn());
        try {
            wareSkuService.unLockStock(unLockStockTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //ack响应失败 重新入队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
